﻿Imports Abyx.SVCMS.Library.DB
Imports Abyx.WindowsExtendedControls

Public Class SQC_CatalogItemFeatures_Form
    Implements ICarfDataEditingChildWindow

#Region "Declarations"
    Dim iMainDataObject As CatalogItemType = Nothing
    Dim iDataObject As FeaturesTypeCollection = Nothing
#End Region

#Region "Overloads/Overrides"

    Public Overloads Sub Show(FormActionOnData As FormActionOnDataEnum, _
                              MainDataObject As CatalogItemType)
        Me.Show()
        If MainDataObject IsNot Nothing Then
            Me.iMainDataObject = MainDataObject
            Me.iDataObject = MainDataObject.Features.Clone
        Else
            Me.Close()
        End If
        Refresh_ListControls()
        Refresh_ControlsWithDataObject()
        Refresh_WindowTitle()
    End Sub

#End Region

#Region "Events"
    Private Sub SQC_CatalogItemFeatures_Form_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Me.InitializeThisFormAndControlSettings()
    End Sub
    Private Sub SQC_CatalogItemFeatures_Form_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Me.InitializeThisFormAndControlSettings(True)
    End Sub
    Private Sub Features_ActionButtonClicked(Node As cTreeViewNode, actionButton As ActionButtonTypesEnum) Handles Features_CTreeView.ActionButtonClicked
        Dim feature As FeaturesType = Node.DataObject
        Me.iDataObject.Remove(feature)
        RefreshFeatureTree()
    End Sub

    Private Sub FeatureCategory_StylishComboBox_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles FeatureCategory_StylishComboBox.SelectedIndexChanged
        Refresh_FeatureSubCategories()
        Refresh_FeatureNames()
        Refresh_FeatureData()
    End Sub

    Private Sub FeatureSubCategory_StylishComboBox_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles FeatureSubCategory_StylishComboBox.SelectedIndexChanged
        Refresh_FeatureNames()
        Refresh_FeatureData()
    End Sub

    Private Sub FeatureName_StylishComboBox_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles FeatureName_StylishComboBox.SelectedIndexChanged
        Refresh_FeatureData()
    End Sub

    Private Sub FeatureAdd_ImageButton_Click(sender As System.Object, e As System.EventArgs) Handles FeatureAdd_ImageButton.Click

        'Do validation
        If FeatureCategory_StylishComboBox.Text = "" Then
            AMsg.ShowDialog("A category selection is a must...", MsgBoxStyle.Critical)
            Exit Sub
        End If
        If FeatureName_StylishComboBox.Text = "" Then
            AMsg.ShowDialog("Feature name should be specified...", MsgBoxStyle.Critical)
            Exit Sub
        End If
        If FeatureData_StylishComboBox.Text = "" Then
            AMsg.ShowDialog("Feature data should be specified", MsgBoxStyle.Critical)
            Exit Sub
        End If

        'Add the new feature
        Dim feature As New FeaturesType(Guid.NewGuid)
        With feature
            .EquipmentId = Me.iMainDataObject.EquipmentId
            .Category = FeatureCategory_StylishComboBox.Text
            .SubCategory = FeatureSubCategory_StylishComboBox.Text
            .FeatureName = FeatureName_StylishComboBox.Text
            .FeatureValue = FeatureData_StylishComboBox.Text
        End With
        Me.iDataObject.Add(feature)

        'now refresh tree
        RefreshFeatureTree()

    End Sub

    Private Sub Cancel_Button_Click(sender As System.Object, e As System.EventArgs) Handles Cancel_Button.Click
        Me.Close()
    End Sub

    Private Sub Commit_Button_Click(sender As System.Object, e As System.EventArgs) Handles Commit_Button.Click
        Me.iMainDataObject.Features = Me.iDataObject
        Me.Close()
    End Sub

#End Region

#Region "ICARF Implementations"
    Public Sub OrderRefreshRelatedDataControls(Optional DataItem As Object = Nothing, Optional ActionOnDataItem As FormActionOnDataEnum = FormActionOnDataEnum.None) Implements ICarfDataEditingChildWindow.OrderRefreshRelatedDataControls

    End Sub
    Public Sub ProcessRefreshRelatedDataControls(sender As System.Windows.Forms.Form, DataTypeCategory As RelatedControlDataCategoriesEnum, Optional DataItem As Object = Nothing, Optional ActionOnDataItem As FormActionOnDataEnum = FormActionOnDataEnum.None) Implements ICarfDataEditingChildWindow.ProcessRefreshRelatedDataControls

    End Sub
    Public Function GetFormInstanceForDataObject(DataObject As Object) As System.Windows.Forms.Form Implements ICarfDataEditingChildWindow.GetFormInstanceForDataObject
        Return Nothing
    End Function
    Public Sub Refresh_ControlsWithDataObject() Implements ICarfDataEditingChildWindow.Refresh_ControlsWithDataObject
        'Fillup features treeview
        RefreshFeatureTree()
    End Sub
    Public Sub Refresh_DataObjectWithControls() Implements ICarfDataEditingChildWindow.Refresh_DataObjectWithControls

    End Sub
    Public Sub InitializeThisFormAndControlSettings(Optional OrderUpdateToFile As Boolean = False) Implements ICarfDataEditingChildWindow.InitializeThisFormAndControlSettings
       'init general settings
        AppSettings.InitFormSettings(Me, OrderUpdateToFile)
    End Sub
#End Region

#Region "Private Procedures"
    Enum formatTypesEnum
        root
        Category
        SubCategory
        FeatureName
        FeatureValue
    End Enum
    Enum cTreeNodeTypesEnum
        Root
        Category
        SubCategory
        FName
        FValue
    End Enum
    Private Sub FormatTreeNode(node As TreeNode, formatType As formatTypesEnum)
        Select Case formatType
            Case Is = formatTypesEnum.root
                'node.NodeFont = New Font(Me.Features_TreeView.Font, FontStyle.Bold)
                node.BackColor = Color.Black
                node.ForeColor = Color.White
            Case Is = formatTypesEnum.Category
                'node.NodeFont = New Font(Me.Features_TreeView.Font, FontStyle.Bold)
                node.BackColor = Color.Blue
                node.ForeColor = Color.Yellow
            Case Is = formatTypesEnum.SubCategory
                node.NodeFont = New Font(Me.Features_CTreeView.Font, FontStyle.Bold)
                node.BackColor = Color.Cornsilk
            Case Is = formatTypesEnum.FeatureName
                'node.NodeFont = New Font(Me.Features_TreeView.Font, FontStyle.Bold + FontStyle.Underline)
                node.BackColor = Color.Cornsilk
                'Case Is = formatTypesEnum.FeatureValue
        End Select
    End Sub
    Private Function SearchTreeNode(NodeText As String, _
                                    NodeType As cTreeNodeTypesEnum, _
                                    node As TreeNode, _
                                    createWithParentNode As cTreeViewNode) As cTreeViewNode

        Dim foundNode As TreeNode = Nothing
        For Each n As TreeNode In node.Nodes
            If n.Text = NodeText Then
                foundNode = n
                Exit For
            Else
                foundNode = SearchTreeNodeRec(NodeText, n)
            End If
        Next

        If foundNode Is Nothing Then
            foundNode = New cTreeViewNode(NodeText, NodeType)
            createWithParentNode.Nodes.Add(foundNode)
        End If

        Return foundNode
    End Function
    Private Function SearchTreeNodeRec(NodeText As String, node As TreeNode) As TreeNode
        For Each n As TreeNode In node.Nodes
            If n.Text = NodeText Then
                Return n
            Else
                Return SearchTreeNodeRec(NodeText, n)
            End If
        Next
        Return Nothing
    End Function
    Private Sub RefreshFeatureTree()

        'Clear all
        Me.Features_CTreeView.Nodes.Clear()

        'Add root node
        Dim rootNode As cTreeViewNode = Nothing
        If Me.iMainDataObject.Name <> "" Then
            rootNode = New cTreeViewNode(Me.iMainDataObject.Name, cTreeNodeTypesEnum.Root)
        Else
            rootNode = New cTreeViewNode("Device Features List", cTreeNodeTypesEnum.Root)
        End If
        FormatTreeNode(rootNode, formatTypesEnum.root)
        Me.Features_CTreeView.Nodes.Add(rootNode)

        'Fillup features treeview
        If Me.iDataObject.Count > 0 Then
            For i As Integer = 0 To Me.iDataObject.Count - 1
                Dim feature As FeaturesType = Me.iDataObject.Item(i)
                If feature.Category <> "" Then
                    'Search for category
                    Dim catNode As cTreeViewNode = SearchTreeNode(feature.Category, cTreeNodeTypesEnum.Category, rootNode, rootNode)
                    FormatTreeNode(catNode, formatTypesEnum.Category)
                    If feature.SubCategory <> "" Then
                        Dim subCatNode As cTreeViewNode = SearchTreeNode(feature.SubCategory, cTreeNodeTypesEnum.SubCategory, catNode, catNode)
                        FormatTreeNode(subCatNode, formatTypesEnum.SubCategory)
                        'Search for feature name and add values
                        Dim fNode As cTreeViewNode = SearchTreeNode(feature.FeatureName, cTreeNodeTypesEnum.FName, subCatNode, subCatNode)
                        FormatTreeNode(fNode, formatTypesEnum.FeatureName)
                        Dim valNode As New cTreeViewNode(feature.FeatureValue, cTreeNodeTypesEnum.FValue, ActionButtonTypesEnum.Cancel, feature)
                        fNode.Nodes.Add(valNode)
                    Else
                        'Search for feature name and add values
                        Dim fNode As cTreeViewNode = SearchTreeNode(feature.FeatureName, cTreeNodeTypesEnum.FName, catNode, catNode)
                        FormatTreeNode(fNode, formatTypesEnum.FeatureName)
                        Dim valNode As New cTreeViewNode(feature.FeatureValue, cTreeNodeTypesEnum.FValue, ActionButtonTypesEnum.Cancel, feature)
                        fNode.Nodes.Add(valNode)
                    End If
                Else
                    'Search for feature name and add values
                    Dim fNode As cTreeViewNode = SearchTreeNode(feature.FeatureName, cTreeNodeTypesEnum.FName, rootNode, rootNode)
                    FormatTreeNode(fNode, formatTypesEnum.FeatureName)
                    Dim valNode As New cTreeViewNode(feature.FeatureValue, cTreeNodeTypesEnum.FValue, ActionButtonTypesEnum.Cancel, feature)
                    fNode.Nodes.Add(valNode)
                End If
            Next
        End If

        'Expand tree items
        rootNode.ExpandAll()

    End Sub
    Private Sub Refresh_WindowTitle()
        If Me.iMainDataObject.Name = "" Then
            Me.Text = "Device features"
        Else
            Me.Text = "Device features for " & Me.iMainDataObject.Name
        End If
    End Sub
    Private Sub Refresh_ListControls()

        Refresh_FeatureCategories()

        Refresh_FeatureSubCategories()

        Refresh_FeatureNames()

        Refresh_FeatureData()

    End Sub
    Private Sub Refresh_FeatureCategories()

        Dim previousSelection As String

        'Refresh Category list
        previousSelection = ""
        If FeatureCategory_StylishComboBox.SelectedItem IsNot Nothing Then _
            previousSelection = FeatureCategory_StylishComboBox.SelectedItem
        FeatureCategory_StylishComboBox.Items.Clear()
        Dim categories = (From sCat As FeaturesType In Me.iMainDataObject.Features _
                          Select sCat.Category _
                          Order By Category).Distinct
        If (categories IsNot Nothing) AndAlso (categories.Count > 0) Then
            For i As Integer = 0 To categories.Count - 1
                If categories(i) IsNot Nothing Then FeatureCategory_StylishComboBox.Items.Add(categories(i))
            Next
        End If
        FeatureCategory_StylishComboBox.SelectedText = previousSelection

    End Sub
    Private Sub Refresh_FeatureSubCategories()

        Dim previousSelection As String

        'Refresh Sub Category list
        previousSelection = ""
        If FeatureSubCategory_StylishComboBox.SelectedItem IsNot Nothing Then _
            previousSelection = FeatureSubCategory_StylishComboBox.SelectedItem
        FeatureSubCategory_StylishComboBox.Items.Clear()
        Dim sucategories As Collections.Generic.IEnumerable(Of String)
        If FeatureCategory_StylishComboBox.SelectedItem <> "" Then
            sucategories = (From sCat As FeaturesType In Me.iMainDataObject.Features _
                            Where sCat.Category = FeatureCategory_StylishComboBox.SelectedItem _
                            Select sCat.SubCategory _
                            Order By SubCategory).Distinct
        Else
            sucategories = (From sCat As FeaturesType In Me.iMainDataObject.Features _
                            Select sCat.SubCategory _
                            Order By SubCategory).Distinct
        End If
        If (sucategories IsNot Nothing) AndAlso (sucategories.Count > 0) Then
            For i As Integer = 0 To sucategories.Count - 1
                If sucategories(i) IsNot Nothing Then FeatureSubCategory_StylishComboBox.Items.Add(sucategories(i))
            Next
        End If
        FeatureSubCategory_StylishComboBox.SelectedItem = previousSelection

    End Sub
    Private Sub Refresh_FeatureNames()

        Dim previousSelection As String

        'Refresh Feature Names list
        previousSelection = ""
        If FeatureName_StylishComboBox.SelectedItem IsNot Nothing Then _
            previousSelection = FeatureName_StylishComboBox.SelectedItem
        FeatureName_StylishComboBox.Items.Clear()
        Dim featureNames As Collections.Generic.IEnumerable(Of String)
        If FeatureCategory_StylishComboBox.SelectedItem <> "" And FeatureSubCategory_StylishComboBox.SelectedItem <> "" Then
            featureNames = (From sCat As FeaturesType In Me.iMainDataObject.Features _
                            Where sCat.Category = FeatureCategory_StylishComboBox.SelectedText _
                                And sCat.SubCategory = FeatureSubCategory_StylishComboBox.SelectedText _
                            Select sCat.FeatureName _
                            Order By FeatureName).Distinct
        ElseIf FeatureCategory_StylishComboBox.SelectedItem <> "" Then
            featureNames = (From sCat As FeaturesType In Me.iMainDataObject.Features _
                            Where sCat.Category = FeatureCategory_StylishComboBox.SelectedItem
                            Select sCat.FeatureName _
                            Order By FeatureName).Distinct
        ElseIf FeatureSubCategory_StylishComboBox.SelectedItem <> "" Then
            featureNames = (From sCat As FeaturesType In Me.iMainDataObject.Features _
                            Where sCat.SubCategory = FeatureSubCategory_StylishComboBox.SelectedItem _
                            Select sCat.FeatureName _
                            Order By FeatureName).Distinct
        Else
            featureNames = (From sCat As FeaturesType In Me.iMainDataObject.Features _
                            Select sCat.FeatureName _
                            Order By FeatureName).Distinct
        End If

        If (featureNames IsNot Nothing) AndAlso (featureNames.Count > 0) Then
            For i As Integer = 0 To featureNames.Count - 1
                If featureNames(i) IsNot Nothing Then FeatureName_StylishComboBox.Items.Add(featureNames(i))
            Next
        End If
        FeatureName_StylishComboBox.SelectedItem = previousSelection

    End Sub
    Private Sub Refresh_FeatureData()

        Dim previousSelection As String

        'Refresh Feature data list
        previousSelection = ""
        If FeatureData_StylishComboBox.SelectedItem IsNot Nothing Then _
            previousSelection = FeatureData_StylishComboBox.SelectedItem
        FeatureData_StylishComboBox.Items.Clear()
        Dim featureData As Collections.Generic.IEnumerable(Of String)

        If FeatureCategory_StylishComboBox.SelectedItem <> "" _
            And FeatureSubCategory_StylishComboBox.SelectedItem <> "" _
            And FeatureName_StylishComboBox.SelectedItem <> "" Then
            featureData = (From sCat As FeaturesType In Me.iMainDataObject.Features _
                           Where sCat.Category = FeatureCategory_StylishComboBox.SelectedItem _
                                And sCat.SubCategory = FeatureSubCategory_StylishComboBox.SelectedItem _
                                And sCat.FeatureName = FeatureName_StylishComboBox.SelectedItem _
                           Select sCat.FeatureValue _
                           Order By FeatureValue).Distinct
        ElseIf FeatureCategory_StylishComboBox.SelectedItem <> "" _
            And FeatureSubCategory_StylishComboBox.SelectedItem <> "" _
            And FeatureName_StylishComboBox.SelectedItem = "" Then
            featureData = (From sCat As FeaturesType In Me.iMainDataObject.Features _
                           Where sCat.Category = FeatureCategory_StylishComboBox.SelectedItem _
                                And sCat.SubCategory = FeatureSubCategory_StylishComboBox.SelectedItem _
                           Select sCat.FeatureValue _
                           Order By FeatureValue).Distinct
        ElseIf FeatureCategory_StylishComboBox.SelectedItem = "" _
            And FeatureSubCategory_StylishComboBox.SelectedItem <> "" _
            And FeatureName_StylishComboBox.SelectedItem <> "" Then
            featureData = (From sCat As FeaturesType In Me.iMainDataObject.Features _
                           Where sCat.SubCategory = FeatureSubCategory_StylishComboBox.SelectedItem _
                                And sCat.FeatureName = FeatureName_StylishComboBox.SelectedItem _
                           Select sCat.FeatureValue _
                           Order By FeatureValue).Distinct
        ElseIf FeatureCategory_StylishComboBox.SelectedItem <> "" _
            And FeatureSubCategory_StylishComboBox.SelectedItem = "" _
            And FeatureName_StylishComboBox.SelectedItem <> "" Then
            featureData = (From sCat As FeaturesType In Me.iMainDataObject.Features _
                           Where sCat.Category = FeatureCategory_StylishComboBox.SelectedItem _
                                And sCat.FeatureName = FeatureName_StylishComboBox.SelectedItem _
                           Select sCat.FeatureValue _
                           Order By FeatureValue).Distinct
        ElseIf FeatureCategory_StylishComboBox.SelectedItem <> "" _
            And FeatureSubCategory_StylishComboBox.SelectedItem = "" _
            And FeatureName_StylishComboBox.SelectedItem = "" Then
            featureData = (From sCat As FeaturesType In Me.iMainDataObject.Features _
                           Where sCat.Category = FeatureCategory_StylishComboBox.SelectedItem _
                           Select sCat.FeatureValue _
                           Order By FeatureValue).Distinct
        ElseIf FeatureCategory_StylishComboBox.SelectedItem = "" _
            And FeatureSubCategory_StylishComboBox.SelectedItem <> "" _
            And FeatureName_StylishComboBox.SelectedItem = "" Then
            featureData = (From sCat As FeaturesType In Me.iMainDataObject.Features _
                           Where sCat.SubCategory = FeatureSubCategory_StylishComboBox.SelectedItem _
                           Select sCat.FeatureValue _
                           Order By FeatureValue).Distinct
        ElseIf FeatureCategory_StylishComboBox.SelectedItem = "" _
            And FeatureSubCategory_StylishComboBox.SelectedItem = "" _
            And FeatureName_StylishComboBox.SelectedItem <> "" Then
            featureData = (From sCat As FeaturesType In Me.iMainDataObject.Features _
                           Where sCat.FeatureName = FeatureName_StylishComboBox.SelectedItem _
                           Select sCat.FeatureValue _
                           Order By FeatureValue).Distinct
        Else
            featureData = (From sCat As FeaturesType In Me.iMainDataObject.Features _
                                Select sCat.FeatureValue _
                                Order By FeatureValue).Distinct
        End If

        If (featureData IsNot Nothing) AndAlso (featureData.Count > 0) Then
            For i As Integer = 0 To featureData.Count - 1
                If featureData(i) IsNot Nothing Then FeatureData_StylishComboBox.Items.Add(featureData(i))
            Next
        End If
        FeatureData_StylishComboBox.SelectedItem = previousSelection

    End Sub
#End Region






End Class


